package ui;

import components.MyFirstLabel;
import components.MyPanel64;
import components.MyPanel8;
import components.MyPanelMouseListener;

import javax.swing.*;
import java.awt.*;

public class MainPanel {
    private static final MainPanel instance = new MainPanel();
    private MainPanel() {

    }
    public static MainPanel getInstance() {
        return instance;
    }

    JFrame frame;
    JPanel panel;

    public JFrame getFrame() {
        return frame;
    }

    public void init(){
        initPanel();
        frame = new JFrame();
        frame.setSize(new Dimension(1200,1100));
        frame.setPreferredSize(new Dimension(1200,1100));
        frame.setTitle("易经-八卦 Copyright 2021 https://gitee.com/chesterccw/yijing.git");
        frame.setContentPane(panel);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static final String[][] GUA_64 = {
            {"0",   "111",    "010",    "100",    "001",    "110",    "101",    "000",    "011"},
            {"111", "111111", "010111", "100111", "001111", "110111", "101111", "000111", "011111"},
            {"010", "111010", "010010", "100010", "001010", "110010", "101010", "000010", "011010"},
            {"100", "111100", "010100", "100100", "001100", "110100", "101100", "000100", "011100"},
            {"001", "111001", "010001", "100001", "001001", "110001", "101001", "000001", "011001"},
            {"110", "111110", "010110", "100110", "001110", "110110", "101110", "000110", "011110"},
            {"101", "111101", "010101", "100101", "001101", "110101", "101101", "000101", "011101"},
            {"000", "111000", "010000", "100000", "001000", "110000", "101000", "000000", "011000"},
            {"011", "111011", "010011", "100011", "001011", "110011", "101011", "000011", "011011"}
    };
    private void initPanel(){
        panel = new JPanel(new GridLayout(9,9));
        for (int i = 0 ; i < 81 ; i++) {
            JPanel tmp;
            if (i == 0) {
                tmp = new JPanel(new GridLayout(4,1));
                tmp.add(new MyFirstLabel("易经为首，"));
                tmp.add(new MyFirstLabel("众经其次。"));
                tmp.add(new MyFirstLabel("中华文化，"));
                tmp.add(new MyFirstLabel("弘扬百世。"));
            } else if (i <= 8 || i % 9 == 0){
                tmp = new MyPanel8(GUA_64[i/9][i%9]);
            } else {
                tmp = new MyPanel64(GUA_64[i/9][i%9]);
            }
            tmp.setBackground(Constant.BACKGROUND_COLOR);
            if (i != 0){
                tmp.addMouseListener(new MyPanelMouseListener());
                tmp.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            tmp.setToolTipText(GUA_64[i/9][i%9]);
            setBorder(tmp);
            panel.add(tmp,i);
        }
    }

    public void setBorder(JPanel panel){
        panel.setBorder(BorderFactory.createLineBorder(
                Constant.BORDER_COLOR));
    }

}
